package com.ticket.sass.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.FilmScheduleSaleChannelMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.FilmScheduleSaleChannelEntity;
import com.ticket.sass.common.entity.tenant.enums.FilmScheduleSaleChannelType;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-29
 */
@Service
public class FilmScheduleSaleChannelServiceImpl extends ServiceImpl<FilmScheduleSaleChannelMapper, FilmScheduleSaleChannelEntity> implements BaseService<FilmScheduleSaleChannelEntity> {
    // 根据filmScheduleIds返回Map，filmScheduleId为键，类型列表为值
    public Map<Long, List<Short>> getChannelsByFilmScheduleIds(List<Long> filmScheduleIds) {
        if (filmScheduleIds.size() == 0){
            return new HashMap<>();
        }
        
        QueryWrapper<FilmScheduleSaleChannelEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("film_schedule_id", filmScheduleIds);
        List<FilmScheduleSaleChannelEntity> channels =  baseMapper.selectList(queryWrapper);
        Map<Long, List<Short>> resultMap = new HashMap<>();
        for (FilmScheduleSaleChannelEntity channel : channels) {
            resultMap.computeIfAbsent(channel.getFilmScheduleId(), k -> new ArrayList<>()).add(channel.getType().getValue());
        }
        return resultMap;
    }
    
    // 传入filmScheduleIds批量新增一条type为0的关联数据
    public boolean addBatchRelationWithDefaultType(List<Long> filmScheduleIds, Long cinemaId, Long tenantId) {
        if (filmScheduleIds.size() == 0){
            return true;
        }
        
        List<FilmScheduleSaleChannelEntity> channels = new ArrayList<>();
        for (Long filmScheduleId : filmScheduleIds) {
            FilmScheduleSaleChannelEntity channel = new FilmScheduleSaleChannelEntity();
            channel.setFilmScheduleId(filmScheduleId);
            channel.setCinemaId(cinemaId);
            channel.setType(FilmScheduleSaleChannelType.WXA); // 设置类型为0
            channel.setTenantId(tenantId);
            channels.add(channel);
        }
        return saveBatch(channels);
    }
    
    // 传入filmScheduleId和类型列表进行修改，新增数据或者删除数据或者不删不减
    public boolean updateChannels(Long filmScheduleId, List<Short> types) {
        QueryWrapper<FilmScheduleSaleChannelEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("film_schedule_id", filmScheduleId);
        List<FilmScheduleSaleChannelEntity> currentChannels =  baseMapper.selectList(queryWrapper);
        Set<Short> currentTypes = currentChannels.stream().map(x -> x.getType().getValue()).collect(Collectors.toSet());
        // 需要新增的类型
        List<Short> addTypes = types.stream().filter(type -> !currentTypes.contains(type)).collect(Collectors.toList());
        // 需要删除的类型
        List<Short> deleteTypes = currentTypes.stream().filter(type -> !types.contains(type)).collect(Collectors.toList());
        
        List<FilmScheduleSaleChannelEntity> addChannels = new ArrayList<>();
        for (Short type : addTypes) {
            FilmScheduleSaleChannelEntity channel = new FilmScheduleSaleChannelEntity(type, filmScheduleId);
            addChannels.add(channel);
        }
        
        boolean addResult = saveBatch(addChannels);
        boolean deleteResult = true;
        if (!deleteTypes.isEmpty()) {
            QueryWrapper<FilmScheduleSaleChannelEntity> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("film_schedule_id", filmScheduleId).in("type", deleteTypes);
            deleteResult = remove(deleteWrapper);
        }
        
        return addResult && deleteResult;
    }
    
    public void removeByScheduleIds(List<Long> sessionCodesToClear) {
        if (sessionCodesToClear.size() == 0){
            return;
        }
        
        this.remove(new QueryWrapper<FilmScheduleSaleChannelEntity>().in("film_schedule_id", sessionCodesToClear));
    }
}
